//
//  UploadStarViewController.m
//  markmap
//
//  Created by 甘世清 on 2024/7/20.
//

#import "UploadStarViewController.h"

@interface UploadStarViewController ()
@property (nonatomic , strong) SQCustomButton * addImage;
@property (nonatomic, strong) SQCustomButton * btn;
@end

@implementation UploadStarViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = UIColor.blackColor;
}
- (void)SQ_UIViewForSelfView{
    UIImageView * starImage = [[UIImageView alloc]init];
    starImage.image = SQGetImage(@"上传截图");
    [self.view addSubview:starImage];
    [starImage mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(self.view);
        make.width.mas_equalTo(SCREEN_WIDETH-40);
        make.height.mas_equalTo(136);
        make.top.equalTo(self.zx_navBar.mas_bottom).offset(10);
    }];
    
    UILabel * xianshi = [[UILabel alloc]init];
    xianshi.text = SQNSLocalString(@"上传评价截图");
    xianshi.font = FONT_MEDIUM(18);
    xianshi.textColor = UIColor.whiteColor;
    [starImage addSubview:xianshi];
    [xianshi mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(starImage).offset(26);
            make.left.equalTo(starImage).offset(16);
    }];
    
    UILabel * label2 = [[UILabel alloc]init];
    label2.text = SQNSLocalString(@"选择并上传您的五星评价截图，\n审核通过后将立即为你开启免\n门票进入");
    label2.font = FONT_REGULAR(13);
    label2.numberOfLines = 0;
    label2.textColor = UIColor.whiteColor;
    [starImage addSubview:label2];
    [label2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(starImage).offset(56);
        make.left.equalTo(starImage).offset(16);
        make.width.mas_equalTo(190);
    }];
    
    [self.view addSubview:self.addImage];
    [self.addImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view);
        make.size.mas_equalTo(SCREEN_WIDETH-40);
        make.top.equalTo(starImage.mas_bottom).offset(24);
    }];
    [self.view addSubview:self.btn];
    [self.btn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view);
        make.width.mas_equalTo(SCREEN_WIDETH-76);
        make.height.mas_equalTo(44);
        make.bottom.equalTo(self.view.mas_bottom).offset(-FootHeight-20);
    }];
    UILabel * topLabel = [[UILabel alloc]init];
    topLabel.text = SQNSLocalString(@"我们会在一个工作日内完成审核");
    topLabel.font = FONT_MEDIUM(14);
    topLabel.textColor = SQColor(@"#58C78F");
    [self.view addSubview:topLabel];
    [topLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(self.view);
            make.top.equalTo(self.addImage.mas_bottom).offset(20);
    }];
    WS(weakSelf);
    [self.addImage setTouchBlock:^(SQCustomButton * _Nonnull button) {
        [weakSelf SQ_rightButtonClick];
    }];
    [self.btn setTouchBlock:^(SQCustomButton * _Nonnull button) {
        [weakSelf SQ_KaiShiRenZheng];
    }];
}

-(void)SQ_KaiShiRenZheng{
    if (self.addImage.SQBackimageView.image ==nil) {
        return;
    }
    WS(weakSelf);
    [YJProgressHUD SQ_showMsgWithoutView:@""];
    [HttpTool SQ_Image:self.addImage.SQBackimageView.image successBlock:^(BaseModel * _Nonnull httpModel) {
        [weakSelf SQ_setUpName:httpModel.data];
    } andFailBloc:^(BaseModel * _Nonnull httpModel, NSError * _Nullable error) {
        [YJProgressHUD showMsgWithoutView:httpModel.message];
    }];
}
-(void)SQ_setUpName:(NSString*)header{
    if (![header isNotEmpty]) {
        [YJProgressHUD showMsgWithoutView:SQNSLocalString(@"图片上传错误")];
        return;
    }
    WS(weakSelf);
    [YJProgressHUD SQ_showMsgWithoutView:@""];
    NSMutableDictionary * dic = [[NSMutableDictionary alloc]init];
    [dic setObject:header forKey:@"img"];
    [HttpTool post:Url59 paramDic:dic successBlock:^(BaseModel * _Nonnull httpModel) {
        [YJProgressHUD showMsgWithoutView:SQNSLocalString(@"上传成功,我们将在1个工作日内完成审核,请耐心等待!")];
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [weakSelf SQ_popViewVC];
        });
    } andFailBloc:^(BaseModel * _Nonnull httpModel, NSError * _Nullable error) {
        [YJProgressHUD showMsgWithoutView:httpModel.message];
    }];
}
-(void)SQ_rightButtonClick{
    WS(weakSelf);
    TZImagePickerController * vc = [[TZImagePickerController alloc]init];
    vc.maxImagesCount = 1;
    vc.allowPickingVideo = NO;
    vc.scaleAspectFillCrop = NO;
    [[Manager SQ_getCurrentViewController] presentViewController:vc animated:YES completion:nil];
    [vc setDidFinishPickingPhotosHandle:^(NSArray<UIImage *> *photos, NSArray *assets, BOOL isSelectOriginalPhoto) {
        for (UIImage * image in photos) {
            [weakSelf SQ_caijian:image];
        }
    }];
}
//上传相册
-(void)SQ_caijian:(UIImage*)image {
   
    self.addImage.SQBackimageView.image = image;
    self.addImage.imageView.hidden = YES;
    self.btn.SQBackimageView.hidden = NO;
    self.btn.textColor = UIColor.whiteColor;
}
- (SQCustomButton *)addImage{
    if (!_addImage) {
        _addImage = [[SQCustomButton alloc]initType:SQCustomButtonTopImageType imageSize:CGSizeMake(SCREEN_WIDETH-40, SCREEN_WIDETH-40) midmargin:0];
        _addImage.imageView.contentMode = UIViewContentModeScaleAspectFill;
        _addImage.SQBackimageView.contentMode = UIViewContentModeScaleAspectFill;
        _addImage.layer.cornerRadius = 12;
        _addImage.clipsToBounds = YES;
        _addImage.image = SQGetImage(@"上传图片 1");
    }
    return _addImage;
}
- (SQCustomButton *)btn{
    if (!_btn) {
       _btn = [[SQCustomButton alloc]initType:SQCustomButtonTopImageType imageSize:CGSizeMake(0, 0) midmargin:0];
        _btn.text = SQNSLocalString(@"立即上传");
        _btn.font = FONT_MEDIUM(17);
        _btn.textColor = SQColor333333;
        _btn.SQBackimageView.image = SQGetImage(@"按钮背景");
        _btn.backgroundColor = SQColor(@"#CECECE");
        _btn.SQBackimageView.hidden = YES;
        _btn.SQBackimageView. contentMode = UIViewContentModeScaleAspectFill;
        _btn.layer.cornerRadius = 22;
        _btn.layer.masksToBounds = YES;
    }
    return _btn;
}

@end
